package com.afishing.servlet;

import com.afishing.dao.ShopDao;
import com.afishing.dao.impl.ShopDaoImpl;
import com.afishing.entity.Shop;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet(name = "GetAllShops", value = "/getAllShops")
public class GetAllShopsServlet extends HttpServlet {
    private final ShopDao shopDao = new ShopDaoImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        ArrayList<Shop> allShop = shopDao.getAllShop();
        //转为json字符串
        String jsonString = JSONObject.toJSONString(allShop);
        response.getWriter().write(jsonString);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
